<?php
namespace Modules\Admin\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;

class AuthRoles extends BaseApiModel
{
    protected $table = 'auth_roles';
    protected $primaryKey = 'id';
    protected $keyType = 'int';
    public $incrementing = true;

    /**
     * @return HasMany
     */
    public function roleMenus(): HasMany
    {
        return $this->hasMany(AuthRoleMenus::class, 'role_id');
    }

    /**
     * @return HasMany
     */
    public function roleDepartments(): HasMany
    {
        return $this->hasMany(AuthRoleDepartments::class, 'role_id');
    }

    /**
     * @param array $menuIds
     */
    public function attachMenus($id, array $menuIds)
    {
        AuthRoleMenus::query()->where(['role_id' => $id])->delete();
        foreach ($menuIds as $menuId) {
            AuthRoleMenus::query()->create([
                'menu_id' => $menuId,
                'role_id' => $id
            ]);
        }
        /* $this->roleMenus()->delete();
        foreach ($menuIds as $menuId) {
            $this->roleMenus()->firstOrCreate([
                'menu_id' => $menuId
            ]);
        } */
    }

    /**
     * @param array $departmentIds
     */
    public function attachDepartments($id, array $departmentIds)
    {
        AuthRoleDepartments::query()->where(['role_id' => $id])->delete();
        foreach ($departmentIds as $departmentId) {
            AuthRoleDepartments::query()->create([
                'department_id' => $departmentId,
                'role_id' => $id
            ]);
        }
        /* $this->roleDepartments()->delete();
        foreach ($departmentIds as $departmentId) {
            $this->roleDepartments()->create([
                'department_id' => $departmentId
            ]);
        } */
    }

    public function delMenusDepartments(int $id)
    {
        AuthRoleMenus::query()->where(['role_id' => $id])->delete();
        AuthRoleDepartments::query()->where(['role_id' => $id])->delete();
    }

    /**
     * @return bool
     */
    public function isSuper(): bool
    {
        return $this->label == 'admin';
    }

    /**
     * @return array
     */
    public function getDepartmentIds(): array
    {
        return $this->roleDepartments()->get(['department_id'])->pluck('department_id')->toArray();
    }

    /**
     * @return array
     */
    public function getMenuIds(): array
    {
        return $this->roleMenus()->get(['menu_id'])->pluck('menu_id')->toArray();
    }

    /**
     * @return Builder[]|Collection
     */
    public function getMenus()
    {
        $menuIds = $this->roleMenus()->get(['menu_id'])->pluck('menu_id')->toArray();
        return AuthRoleMenus::query()->whereIn('id', $menuIds)->get();
    }

    /**
     * @return array
     */
    public function getMenuPerms(): array
    {
        $menuIds = $this->roleMenus()->get(['menu_id'])->pluck('menu_id')->toArray();
        return AuthMenu::query()->whereIn('id', $menuIds)->get(['perms'])->pluck('perms')->filter()->values()->toArray();
    }
    
}
